$(function(){ 

    //评论
    var stars = $(".xingxing-grade li");
    var stepW=$(".xingxing-grade li").width();
    var option = $(".option");
    stars.each(function(i){
        $(stars[i]).on("click",function(e){
            $('.xingxing-grade li:lt('+i+1+')').addClass('active');
            $('.xingxing-grade li:gt('+i+')').removeClass("active");
            $(".xingxing-text li").eq(i).addClass('active').siblings().removeClass("active");
        
        });
    });

	//点击擅长
	$(".evaluate-doctorClick .one span").on("click",function(){
		$(this).toggleClass("active");
	});

	//提交评论
    $("#evaBtn").on("click", submitEvaluateInfo);
    /**
     * 提交评价信息
     */
    function DoctorEvaluate() {
        var doctorEvaluate;
        doctorEvaluate = utilModule.dealFormFilter("evalForm");
        return doctorEvaluate;
    }
    function submitEvaluateInfo() {
        var doctorid = utilModule.getQueryString("doctorid") ? utilModule.getQueryString("doctorid") :mui.alert("未获取到医生id") ;
        var serviceid = utilModule.getQueryString("serviceid")? utilModule.getQueryString("serviceid") :mui.alert("未获取到服务id")  ;
       
        var _score= $(".xingxing-text .active").attr("v");
        var content = $("#content").val();
       
        if(utilModule.checkUndefined(doctorid) == "") {
            mui.alert("医生id不能为空", "提示");
            return false;
        }
        if(utilModule.checkUndefined(serviceid) == "") {
            mui.alert("咨询单id不能为空", "提示");
            return false;
        }
        if(utilModule.checkUndefined(content) == "") {
            mui.alert("评价内容不能为空", "提示");
            return false;
        }
      
        var doctorEvaluate = DoctorEvaluate();
        doctorEvaluate.appraiserid = USERID;
        doctorEvaluate.doctorid = doctorid;
        doctorEvaluate.serviceid = serviceid;
        doctorEvaluate.score=_score;
        doctorEvaluate.content=content;
        doctorEvaluate.type=utilModule.getQueryString("type");
        doctorModule.doctorEvaluate(doctorEvaluate, function(callBack) {
        	/*location.href="../doctor/doctor-details.html?doctorid="+doctorid;*/
            //完成评价,删除 聊天会话
            var index_doctor = JSON.parse(utilModule.getStorage("index_doctor"));
            if(index_doctor !=null){
	            for (var i = index_doctor.doctorlist.length - 1; i >= 0; i--) {
	                if(index_doctor.doctorlist[i]["id"]==doctorid){
	                    index_doctor.doctorlist.splice(i,1) ;
	                    console.log(index_doctor);
	                }
	            }
	            if(index_doctor.doctorlist.length<1){
	                utilModule.removeStorage("index_doctor");
	            }else{
	                utilModule.removeStorage("index_doctor");
	                utilModule.setStorage("index_doctor",JSON.stringify(index_doctor));
	            }
            }
            //清空与该医生之前的聊天记录
            var chatList = utilModule.getStorage("chat_list");
         
            if(chatList != null) {
                var chatObject = JSON.parse(chatList);
                var rd_key = DOCTOR_IM_PREFFIX+ doctorid;
                delete chatObject[rd_key];
                
                var $chatObject = JSON.stringify(chatObject);
                if($chatObject.length ==2){
                    utilModule.removeStorage("chat_list");
                }else{
                    utilModule.setStorage("chat_list", $chatObject);
                }
            }
			mui.alert("您的评价已提交,谢谢","提示");	
        	//location.replace("../../index.html");
        	//window.location = "http://f.xinyuyl.cn/xyyl-patient/n/index.html";
        	window.location = "../../index.html";
        });
    }
});
